package com.ca.ui.core.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.ca.ui.core.R;
import com.ca.ui.core.bean.MainHeader;
import com.ca.ui.core.bean.PaperInfo;
import com.ca.ui.core.linstener.BannerCardOnPageChangeListener;
import com.ca.ui.core.util.ActivityStarter;
import com.ca.ui.core.view.card.PaperCard;
import com.ca.ui.core.view.header.MainHeaderView;

import java.util.ArrayList;
import java.util.List;

public class PaperAdapter extends RecyclerView.Adapter<PaperAdapter.PaperHolder> {

    private final Activity activity;
    private List<PaperInfo> list;

    public PaperAdapter(Activity activity, List<PaperInfo> list) {
        this.list = list == null ? new ArrayList<>() : list;
        this.activity = activity;
    }

    @NonNull
    @Override
    public PaperHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.paper_item_layout, parent, false);
        return new PaperHolder(view);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onBindViewHolder(@NonNull PaperHolder holder, int position) {
        holder.mainHeaderView.setVisibility(View.GONE);
        holder.paperCard.setVisibility(View.VISIBLE);

        PaperInfo paperInfo = list.get(position);
        holder.paperCard.setPaper(paperInfo);
        if (activity != null) {
            holder.paperCard.imageView.setOnClickListener(v -> {
                ActivityStarter.startImageActivity(
                        activity,
                        paperInfo.getPaperImageUrlArray(),
                        0);
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setList(List<PaperInfo> list) {

        if (list == null) {
            list = new ArrayList<>();
        }

        if (!this.list.equals(list)) {
            this.list = list;
            notifyDataSetChanged();
        }
    }

    /**
     * 追加新的数据
     * @param list 传入的列表为新的数据列表
     */
    public void appendList(List<PaperInfo> list) {

        if (list == null) {
            return;
        }

        int startPosition = getItemCount();

        //  添加数据
        for (PaperInfo info : list) {
            if (!this.list.contains(info)) {
                this.list.add(info);
            }
        }

        //  如果没有新的数据添加，弹出提示
        if (startPosition == this.list.size()) {
            Toast.makeText(activity, "暂时没有更多数据了", Toast.LENGTH_SHORT).show();
        }

        notifyItemRangeInserted(startPosition, this.list.size() - startPosition);
    }

    public static class PaperHolder extends RecyclerView.ViewHolder {

        public MainHeaderView mainHeaderView;
        public PaperCard paperCard;

        public PaperHolder(@NonNull View itemView) {
            super(itemView);
            mainHeaderView = itemView.findViewById(R.id.mainHeader);
            paperCard = itemView.findViewById(R.id.card_paper);
        }
    }
}
